using micromsg;
using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Scene
{
	public class NetSceneSendMsg : NetSceneBaseEx<SendMsgRequest, SendMsgResponse, SendMsgRequest.Builder>
	{
		public const int MM_MSG_EMOJI_QQ = 1;
		public const int MM_MSG_EMOJI_EMOJI = 2;
		public const int MM_MSG_EMOJI_ART = 4;
		public const int MAX_RETRY_COUNT = 1;
		private const string TAG = "NetSceneSendMsg";
		public static int mMsgRetryTimes = 0;
		public static Dictionary<string, ChatMsg> sendingHash = new Dictionary<string, ChatMsg>();
		private Random id_random = new Random();
		private static EventWatcher mWatcherToCleanStatus = null;
		public NetSceneSendMsg()
		{
			NetSceneSendMsg.mWatcherToCleanStatus = new EventWatcher(this, null, new EventHandlerDelegate(NetSceneSendMsg.onHandlerToCleanStatus));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SYNC, NetSceneSendMsg.mWatcherToCleanStatus);
		}
		private static void onHandlerToCleanStatus(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			NetSceneSyncResult netSceneSyncResult = evtArgs.mObject as NetSceneSyncResult;
			if (netSceneSyncResult == null)
			{
			}
			if (netSceneSyncResult == null || netSceneSyncResult.syncStatus != SyncStatus.syncEnd || netSceneSyncResult.syncCount != 0)
			{
				return;
			}
			List<ChatMsg> list = StorageMgr.chatMsg.searhCacheMsg(1, 0, 1);
			List<ChatMsg> list2 = StorageMgr.chatMsg.searhCacheMsg(36, 0, 1);
			if (list2 != null && list2.get_Count() > 0)
			{
				if (list == null)
				{
					list = new List<ChatMsg>();
				}
				using (List<ChatMsg>.Enumerator enumerator = list2.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatMsg current = enumerator.get_Current();
						list.Add(current);
					}
				}
			}
			if (list != null && list.get_Count() > 0)
			{
				ServiceCenter.sceneSendMsg.sendCachMsg(list);
				if (NetSceneSendMsg.mWatcherToCleanStatus != null)
				{
					EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SYNC, NetSceneSendMsg.mWatcherToCleanStatus);
					NetSceneSendMsg.mWatcherToCleanStatus = null;
				}
				return;
			}
		}
		public void sendCachMsg(List<ChatMsg> msgList)
		{
			using (List<ChatMsg>.Enumerator enumerator = msgList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ChatMsg current = enumerator.get_Current();
					if (!NetSceneSendMsg.sendingHash.ContainsKey(current.strClientMsgId))
					{
						NetSceneSendMsg.sendingHash.Add(current.strClientMsgId, current);
					}
				}
			}
			this.doScene(msgList, 1);
		}
		public ChatMsg buildChatMsg(string talkerName, string msgContent, int msgType = 1)
		{
			ChatMsg chatMsg = new ChatMsg();
			chatMsg.strMsg = msgContent;
			chatMsg.strTalker = talkerName;
			chatMsg.nMsgType = msgType;
			chatMsg.nStatus = 0;
			chatMsg.nCreateTime = (long)Util.getNowMilliseconds() / 1000L;
			chatMsg.nIsSender = 1;
			chatMsg.strClientMsgId = MD5Core.GetHashString(string.Concat(new object[]
			{
				chatMsg.strTalker,
				this.id_random.Next(),
				msgContent,
				Util.getNowMilliseconds()
			}));
			return chatMsg;
		}
		public string getContactCardContent(Contact contact_0)
		{
			if (contact_0 == null)
			{
				return null;
			}
			return string.Concat(new object[]
			{
				"<msg username=\"",
				contact_0.strUsrName,
				"\" nickname=\"",
				contact_0.strNickName.htmlString(),
				"\" fullpy=\"",
				contact_0.strQuanPin,
				"\" shortpy=\"",
				contact_0.strPYInitial,
				"\" imagestatus=\"",
				contact_0.nImgFlag,
				"\" scene=\"17\" province=\"",
				contact_0.strProvince,
				"\" city=\"",
				contact_0.strCity,
				"\" sign=\"",
				contact_0.strSignature.htmlString(),
				"\" percard=\"",
				contact_0.nPersonalCard,
				"\" sex=\"",
				contact_0.nSex,
				"\" alias=\"",
				contact_0.strAlias.htmlString(),
				"\"  certflag=\"",
				contact_0.nVerifyFlag,
				"\"  certinfo=\"",
				contact_0.strVerifyInfo.htmlString(),
				"\"  brandHomeUrl=\"",
				contact_0.strBrandExternalInfo.htmlString(),
				"\"  brandIconUrl=\"",
				contact_0.strBrandIconURL.htmlString(),
				"\" />"
			});
		}
		public string getMapMsgContent(double latitudeParam, double longitudeParam, int scale, string address)
		{
			return string.Concat(new object[]
			{
				"<msg><location x=\"",
				latitudeParam,
				"\" y=\"",
				longitudeParam,
				"\" scale=\"",
				scale,
				"\" label=\"",
				address.htmlString(),
				"\" maptype=\"roadmap\"/></msg>"
			});
		}
		private bool isMsgValid(ChatMsg chatMsgInfo)
		{
			if (chatMsgInfo != null && !string.IsNullOrEmpty(chatMsgInfo.strTalker) && !string.IsNullOrEmpty(chatMsgInfo.strMsg) && !string.IsNullOrEmpty(chatMsgInfo.strClientMsgId))
			{
				if (chatMsgInfo.nMsgType == 1 || chatMsgInfo.nMsgType == 36 || chatMsgInfo.nMsgType == 42 || chatMsgInfo.nMsgType == 48)
				{
					return true;
				}
			}
			return false;
		}
		public void doSendMsg(ChatMsg chatMsgInfo, int emojiType = 1)
		{
			if (!this.isMsgValid(chatMsgInfo))
			{
				return;
			}
			if (NetSceneSendMsg.sendingHash.ContainsKey(chatMsgInfo.strClientMsgId))
			{
				return;
			}
			NetSceneSendMsg.sendingHash.Add(chatMsgInfo.strClientMsgId, chatMsgInfo);
			chatMsgInfo.nStatus = 0;
			if (!StorageMgr.chatMsg.addMsg(chatMsgInfo))
			{
				return;
			}
			this.autoReSendMsg(chatMsgInfo, emojiType);
		}
		private void autoReSendMsg(ChatMsg chatMsg_0, int emojiType)
		{
			List<ChatMsg> list = new List<ChatMsg>();
			list.Add(chatMsg_0);
			this.doScene(list, emojiType);
		}
		public bool doSceneResendMsg(ChatMsg chatMsgInfo)
		{
			if (!this.isMsgValid(chatMsgInfo))
			{
				return false;
			}
			if (NetSceneSendMsg.sendingHash.ContainsKey(chatMsgInfo.strClientMsgId))
			{
				return false;
			}
			NetSceneSendMsg.sendingHash.Add(chatMsgInfo.strClientMsgId, chatMsgInfo);
			if (chatMsgInfo.nStatus != 0)
			{
				chatMsgInfo.nStatus = 0;
				StorageMgr.chatMsg.updateMsg(chatMsgInfo);
			}
			List<ChatMsg> list = new List<ChatMsg>();
			list.Add(chatMsgInfo);
			return this.doScene(list, 1);
		}
		private bool doScene(List<ChatMsg> chatMsgList, int emojiType = 1)
		{
			return new NetSceneSendMsg().doSceneEx(chatMsgList, emojiType);
		}
		private bool doSceneEx(List<ChatMsg> chatMsgList, int emojiType = 1)
		{
			if (chatMsgList != null && chatMsgList.get_Count() > 0)
			{
				base.beginBuilder();
				this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(2);
				for (int i = 0; i < chatMsgList.get_Count(); i++)
				{
					ChatMsg arg_38_0 = chatMsgList.get_Item(i);
					MicroMsgRequest.Builder builder = MicroMsgRequest.CreateBuilder();
					builder.SetCreateTime((uint)chatMsgList.get_Item(i).nCreateTime);
					if (chatMsgList.get_Item(i).strMsg == null)
					{
						chatMsgList.get_Item(i).strMsg = "";
					}
					builder.SetContent(chatMsgList.get_Item(i).strMsg);
					if (chatMsgList.get_Item(i).strClientMsgId == null)
					{
						chatMsgList.get_Item(i).strClientMsgId = "";
					}
					builder.SetClientMsgId(chatMsgList.get_Item(i).strClientMsgId);
					builder.SetFromUserName(Util.toSKString(AccountMgr.curUserName));
					builder.SetToUserName(Util.toSKString(chatMsgList.get_Item(i).strTalker));
					builder.SetType((uint)chatMsgList.get_Item(i).nMsgType);
					builder.SetEmojiFlag((uint)emojiType);
					MicroMsgRequest microMsgRequest = builder.Build();
					this.mBuilder.ListList.Add(microMsgRequest);
				}
				this.mBuilder.Count = (uint)this.mBuilder.ListList.get_Count();
				this.mSessionPack.mCmdID = 2;
				base.endBuilder();
				return true;
			}
			Log.smethod_1("NetSceneSendMsg", "sendMsg list is null");
			return false;
		}
		private void doSendMsgFailProc(SendMsgRequest request, SendMsgResponse response)
		{
			List<ChatMsg> list = new List<ChatMsg>();
			if (response != null && response.Count > 0u)
			{
				int num = 0;
				while ((long)num < (long)((ulong)response.Count))
				{
					string clientMsgId = response.GetList(num).ClientMsgId;
					if (!string.IsNullOrEmpty(clientMsgId) && NetSceneSendMsg.sendingHash.ContainsKey(clientMsgId))
					{
						ChatMsg chatMsg = NetSceneSendMsg.sendingHash.get_Item(clientMsgId);
						NetSceneSendMsg.sendingHash.Remove(chatMsg.strClientMsgId);
						if (chatMsg != null)
						{
							chatMsg.nStatus = 1;
							list.Add(chatMsg);
						}
					}
					num++;
				}
			}
			else
			{
				if (request != null && request.Count > 0u)
				{
					int num2 = 0;
					while ((long)num2 < (long)((ulong)request.Count))
					{
						string clientMsgId2 = request.GetList(num2).ClientMsgId;
						if (!string.IsNullOrEmpty(clientMsgId2) && NetSceneSendMsg.sendingHash.ContainsKey(clientMsgId2))
						{
							ChatMsg chatMsg2 = NetSceneSendMsg.sendingHash.get_Item(clientMsgId2);
							NetSceneSendMsg.sendingHash.Remove(chatMsg2.strClientMsgId);
							if (chatMsg2 != null)
							{
								chatMsg2.nStatus = 1;
								list.Add(chatMsg2);
							}
						}
						num2++;
					}
				}
			}
			StorageMgr.chatMsg.updateMsgList(list);
		}
		protected override void onFailed(SendMsgRequest request, SendMsgResponse response)
		{
			PackResult lastError = base.getLastError();
			if (lastError == PackResult.PACK_TIMEOUT && NetSceneSendMsg.mMsgRetryTimes < 1)
			{
				Log.smethod_1("NetSceneSendMsg", "doScene: send message timeout! retry doscene mMsgRetryTimes = " + NetSceneSendMsg.mMsgRetryTimes);
				if (request != null && request.Count > 0u)
				{
					int num = 0;
					while ((long)num < (long)((ulong)request.Count))
					{
						string clientMsgId = request.GetList(num).ClientMsgId;
						if (!string.IsNullOrEmpty(clientMsgId) && NetSceneSendMsg.sendingHash.ContainsKey(clientMsgId))
						{
							ChatMsg chatMsg = NetSceneSendMsg.sendingHash.get_Item(clientMsgId);
							if (chatMsg != null)
							{
								List<ChatMsg> list = new List<ChatMsg>();
								list.Add(chatMsg);
								this.doScene(list, 1);
							}
						}
						num++;
					}
				}
				NetSceneSendMsg.mMsgRetryTimes++;
				return;
			}
			Log.smethod_1("NetSceneSendMsg", "send msg fail because of net error");
			NetSceneSendMsg.mMsgRetryTimes = 0;
			this.doSendMsgFailProc(request, null);
		}
		protected override void onSuccess(SendMsgRequest request, SendMsgResponse response)
		{
			NetSceneSendMsg.mMsgRetryTimes = 0;
			if (response.BaseResponse.Ret != 0)
			{
				Log.smethod_1("NetSceneSendMsg", "send msg fail because of error ret = " + response.BaseResponse.Ret.ToString());
				this.doSendMsgFailProc(request, response);
				return;
			}
			List<ChatMsg> list = new List<ChatMsg>();
			int num = 0;
			while ((long)num < (long)((ulong)response.Count))
			{
				MicroMsgResponse list2 = response.GetList(num);
				if (list2 != null)
				{
					if (list2.Ret != 0)
					{
						Log.smethod_1("NetSceneSendMsg", "error send msg fail ret =  " + list2.Ret);
						string clientMsgId = list2.ClientMsgId;
						if (!string.IsNullOrEmpty(clientMsgId) && NetSceneSendMsg.sendingHash.ContainsKey(clientMsgId))
						{
							ChatMsg chatMsg = NetSceneSendMsg.sendingHash.get_Item(clientMsgId);
							if (chatMsg != null)
							{
								NetSceneSendMsg.sendingHash.Remove(chatMsg.strClientMsgId);
								chatMsg.nStatus = 1;
								StorageMgr.chatMsg.updateMsg(chatMsg);
								if (list2.Ret == -49)
								{
									EventCenter.postEvent(EventConst.ON_NETSCENE_SENDMSG_FAIL, new MsgResultRet(chatMsg, RetConst.MM_ERR_NEED_QQPWD), null);
								}
							}
						}
					}
					else
					{
						string clientMsgId2 = list2.ClientMsgId;
						if (!string.IsNullOrEmpty(clientMsgId2) && NetSceneSendMsg.sendingHash.ContainsKey(clientMsgId2))
						{
							ChatMsg chatMsg2 = NetSceneSendMsg.sendingHash.get_Item(clientMsgId2);
							NetSceneSendMsg.sendingHash.Remove(chatMsg2.strClientMsgId);
							if (chatMsg2 != null)
							{
								chatMsg2.nStatus = 2;
								chatMsg2.nMsgSvrID = (int)list2.MsgId;
								if (list2.CreateTime > 0u)
								{
									chatMsg2.nCreateTime = (long)((ulong)list2.CreateTime);
								}
								list.Add(chatMsg2);
							}
						}
					}
				}
				num++;
			}
			StorageMgr.chatMsg.updateMsgList(list);
			EventCenter.postEvent(EventConst.ON_NETSCENE_SENDMSG_SUCCESS, null, null);
		}
		public void testSendMsg(string usrName, string strToSend, int nRepeatCnt)
		{
			if (usrName != null && strToSend != null && nRepeatCnt != 0)
			{
				List<ChatMsg> list = new List<ChatMsg>();
				Random random = new Random();
				for (int i = 0; i < nRepeatCnt; i++)
				{
					ChatMsg chatMsg = this.buildChatMsg(usrName, strToSend + i, 1);
					list.Add(chatMsg);
					ChatMsg expr_36 = chatMsg;
					expr_36.strClientMsgId += random.Next();
				}
				this.doScene(list, 1);
				return;
			}
		}
	}
}
